import { reservationModelProvider } from '@app/common/constants';
import {
  Reservation,
  ReservationStatus,
} from '@app/common/types/graphqls/graphql.schema';
import { dateFormat } from '@app/common/types/meta/date';
import { Inject, Injectable } from '@nestjs/common';
import * as dayjs from 'dayjs';
import { Model, ObjectId } from 'mongoose';
@Injectable()
export class ReservationService {
  constructor(
    @Inject(reservationModelProvider)
    private readonly reservationModel: Model<Reservation>,
  ) {}

  create(reservation: Reservation): Promise<Reservation> {
    return this.reservationModel.create(reservation);
  }

  update(id: ObjectId, reservation: Reservation) {
    return this.reservationModel.findByIdAndUpdate(id, reservation, {
      new: true,
    });
  }

  updateStatus(id: ObjectId, status: ReservationStatus) {
    return this.reservationModel.findByIdAndUpdate(
      id,
      { status },
      { new: true },
    );
  }

  query(id: ObjectId) {
    return this.reservationModel.findById(id);
  }

  queryBy(date: Date | string, status: ReservationStatus) {
    const startDate = dayjs(date).format(dateFormat);

    const nextDate = dayjs(date).add(1, 'd').format(dateFormat);

    return this.reservationModel.find({
      arrivalTime: { $gte: startDate, $lt: nextDate },
      status,
    });
  }
}
